﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;
using SDT.Lib.Common;
using System.Configuration;
namespace SDT.Model
{
    public class NganluongPaymentManager
    {
        public static string GetMD5Hash(string input)
        {

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();

            byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);

            bs = x.ComputeHash(bs);

            System.Text.StringBuilder s = new System.Text.StringBuilder();

            foreach (byte b in bs)
            {

                s.Append(b.ToString("x2").ToLower());

            }

            String md5String = s.ToString();

            return md5String;
        }
        
        public static Boolean verifyPaymentUrl(string transaction_info, string order_code, string price, string payment_id, string payment_type, string error_text, string secure_code)
        {
            string nganluong_url = NganluongPayment.Url;
            string merchant_site_code = NganluongPayment.MerchantSiteCode;	//thay ma merchant site ma ban da dang ky vao day
            string secure_pass = NganluongPayment.SecurePass;	//thay mat khau giao tiep giua website cua ban voi NganLuong.vn ma ban da dang ky vao day
        
            // Tao ma xac thuc
            string str = "";

            str += " " + transaction_info;

            str += " " + order_code;

            str += " " + price;

            str += " " + payment_id;

            str += " " + payment_type;

            str += " " + error_text;

            str += " " + merchant_site_code;

            str += " " + secure_pass;

            // Ma hoa cac tham so
            string verify_secure_code = "";

            verify_secure_code = GetMD5Hash(str);

            // Xac thuc ma
            if (verify_secure_code == secure_code) return true;

            return false;
        }

        public static String buildCheckoutUrl(String return_url, String receiver, String transaction_info, String order_code, String price)
        {
            //var href = "https://www.nganluong.vn/button_payment.php?receiver=" + receiver + "&product_name=" + order_code + "&price=" + price + "&return_url=" + return_url + "&comments=" + transaction_info;
            //return href;
            string nganluong_url = NganluongPayment.Url;
            string merchant_site_code = NganluongPayment.MerchantSiteCode;	//thay ma merchant site ma ban da dang ky vao day
            string secure_pass = NganluongPayment.SecurePass;	//thay mat khau giao tiep giua website cua ban voi NganLuong.vn ma ban da dang ky vao day
        
            String secure_code = "";

            secure_code += merchant_site_code;

            secure_code += " " + HttpUtility.UrlEncode(return_url).ToLower();

            secure_code += " " + receiver;

            secure_code += " " + transaction_info;

            secure_code += " " + order_code;

            secure_code += " " + price;

            secure_code += " " + secure_pass;

            // T?o m?ng bam
            Hashtable ht = new Hashtable();

            ht.Add("merchant_site_code", merchant_site_code);

            ht.Add("return_url", HttpUtility.UrlEncode(return_url).ToLower());

            ht.Add("receiver", receiver);

            ht.Add("transaction_info", transaction_info);

            ht.Add("order_code", order_code);

            ht.Add("price", price);

            ht.Add("secure_code", GetMD5Hash(secure_code));

            // T?o url redirect
            String redirect_url = nganluong_url;

            if (redirect_url.IndexOf("?") == -1)
            {
                redirect_url += "?";
            }
            else if (redirect_url.Substring(redirect_url.Length - 1, 1) != "?" && redirect_url.IndexOf("&") == -1)
            {
                redirect_url += "&";
            }

            String url = "";

            // Duy?t các ph?n t? trong m?ng bam ht1 d? t?o redirect url
            IDictionaryEnumerator en = ht.GetEnumerator();

            while (en.MoveNext())
            {
                if (url == "")
                    url += en.Key.ToString() + "=" + en.Value.ToString();
                else
                    url += "&" + en.Key.ToString() + "=" + en.Value.ToString();
            }

            String rdu = redirect_url + url;

            return rdu;
        }
    }
}
